package com.kimoge.spring6.resources.resource;

import org.springframework.core.io.UrlResource;

import java.io.InputStream;
import java.net.MalformedURLException;

/**
 * @author kimoge
 * @date 2025/8/30
 * @description 测试UrlResource类
 */
public class UrlResourceDemo {
    public static void main(String[] args) {
        //1、访问网络资源
//        String urlPath = "https://www.baidu.com";
        //2、访问文件系统资源
        String filePath = "file:properties.txt";
        loadResource(filePath);
    }

    public static void loadResource(String path) {
        try {
            UrlResource urlResource = new UrlResource(path);
            System.out.println(urlResource.getURL());
            System.out.println(urlResource.getURI());
            System.out.println(urlResource.getDescription());
            System.out.println("---------fileName---------");
            System.out.println(urlResource.getFilename());
            // 获取资源内容
            System.out.println("---------资源内容---------");
            InputStream inputStream = urlResource.getInputStream();
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            System.out.println(new String(bytes));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
